package com.ruoyi.law.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.law.domain.dto.LawTeamDTO;
import com.ruoyi.law.domain.entity.LawLawyer;
import com.ruoyi.law.domain.entity.LawService;
import com.ruoyi.law.domain.entity.LawTeam;
import com.ruoyi.law.domain.entity.LawTeamService;
import com.ruoyi.law.mapper.LawTeamMapper;
import com.ruoyi.law.service.ILawLawyerService;
import com.ruoyi.law.service.ILawServiceService;
import com.ruoyi.law.service.ILawTeamService;
import com.ruoyi.law.service.ILawTeamServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author code
 * @since 2022-11-30
 */
@Service
public class LawTeamServiceImpl extends ServiceImpl<LawTeamMapper, LawTeam> implements ILawTeamService {

    /**
     * 团队服务关系表
     */
    @Autowired
    private ILawTeamServiceService teamServiceRelation;
    @Autowired
    private ILawLawyerService lawyerService;
    @Autowired
    private ILawServiceService serviceService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveDto(LawTeamDTO lawTeamDTO) {
        LawTeam lawTeam = BeanUtil.toBean(lawTeamDTO, LawTeam.class);
        lawTeam.setId(IdUtil.getSnowflakeNextId());
        // 保存关系表
        saveBatchRelation(lawTeam.getId(), lawTeamDTO.getServiceIds());

        return super.save(lawTeam);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateDto(LawTeamDTO lawTeamDTO) {
        LawTeam lawTeam = BeanUtil.toBean(lawTeamDTO, LawTeam.class);
        // 先删除
        teamServiceRelation.removeByTeamId(lawTeam.getId());
        // 保存关系表
        saveBatchRelation(lawTeam.getId(), lawTeamDTO.getServiceIds());

        return super.updateById(lawTeam);
    }

    /**
     * 批量保存关系表
     *
     * @param lawTeamId  法律团队id
     * @param serviceIds 服务id
     */
    private void saveBatchRelation(Long lawTeamId, String serviceIds) {
        if (StrUtil.isNotBlank(serviceIds)) {
            String[] split = serviceIds.split(",");
            List<String> strings = Arrays.asList(split);
            // List<String>转List<Long>
            List<Long> serviceIdList = strings.stream().map(Long::parseLong).collect(Collectors.toList());
            List<LawTeamService> lawTeamServices = serviceIdList.stream().map(serviceId -> {
                LawTeamService lawTeamService = new LawTeamService();
                lawTeamService.setServiceId(serviceId);
                lawTeamService.setTeamId(lawTeamId);
                return lawTeamService;
            }).collect(Collectors.toList());
            teamServiceRelation.saveBatch(lawTeamServices);
        }
    }

    @Override
    public LawTeamDTO getDtoById(Long id) {
        LawTeam lawTeam = super.getById(id);
        LawTeamDTO lawTeamDTO = BeanUtil.toBean(lawTeam, LawTeamDTO.class);
        // 查询关系表
        List<LawTeamService> serviceList = teamServiceRelation.listByTeamId(id);
        if (CollectionUtil.isNotEmpty(serviceList)) {
            List<Long> serviceIds = serviceList.stream().map(LawTeamService::getServiceId).collect(Collectors.toList());
            LambdaQueryWrapper<LawService> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(LawService::getId, serviceIds);
            lawTeamDTO.setServiceList(serviceService.list(queryWrapper));
            // serviceIds转serviceStringIds
            String serviceStringIds = serviceIds.stream().map(String::valueOf).collect(Collectors.joining(","));
            lawTeamDTO.setServiceIds(serviceStringIds);
        }
        // 律师申请信息
        LawLawyer lawLawyer = lawyerService.getById(lawTeam.getLawyarId());
        lawTeamDTO.setLawLawyer(lawLawyer);
        return lawTeamDTO;
    }
}
